package cn.kgc.controller;

import cn.kgc.pojo.PersonnelJ;
import cn.kgc.pojo.SalaryPO;
import cn.kgc.service.SalaryStandardService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Controller
public class SalaryStandardController {
    @Resource
    private SalaryStandardService salaryStandardService;

    @RequestMapping("/salaryStandard")
    public String SalaryStandard() {
        return "SalaryStandard";
    }

    //分页模糊查询
    @RequestMapping("/findAll")
    @ResponseBody
    public String findAll(@RequestParam(value = "salaryId", required = false) String salaryId,
                          @RequestParam("fileStatus") String fileStatus,
                          @RequestParam("inputtingTime1") String inputtingTime1,
                          @RequestParam("inputtingTime2") String inputtingTime2,
                          @RequestParam("currentPage") Integer currentPage,
                          @RequestParam("pageSize") Integer pageSize) {
        //核心点
        PageHelper.startPage(currentPage, pageSize);
        //得到分页查询的数据
        List<SalaryPO> list = salaryStandardService.findAll(salaryId, fileStatus, inputtingTime1, inputtingTime2);
        //非常合理化的去转换分页数据，携带总条数
        PageInfo<SalaryPO> personnelPageInfo = new PageInfo<SalaryPO>(list);
        System.out.println("总条数" + personnelPageInfo.getTotal());
        return JSONObject.toJSONStringWithDateFormat(personnelPageInfo, "yyyy-MM-dd");
    }

    /**
     * 添加页面跳转
     */
    @RequestMapping("/addSalary")
    public String addSalary() {
        return "SalaryStandardAdd";
    }

    /**
     * 新增
     * @param salaryPO
     * @return
     */
    @RequestMapping("/addSalaryStandard")
    @ResponseBody
    public boolean addSalaryStandard(@ModelAttribute SalaryPO salaryPO) {
        double caculateSum = getCaculateSum(salaryPO);
        salaryPO.setGross(caculateSum);
        return salaryStandardService.addSalaryStandard(salaryPO);
    }
    //计算
    private double getCaculateSum(SalaryPO salaryParam) {
        BigDecimal baseDate = new BigDecimal(Double.toString(salaryParam.getBase()));
        BigDecimal bonusDate = new BigDecimal(Double.toString(salaryParam.getBonus()));
        BigDecimal trafficDate = new BigDecimal(Double.toString(salaryParam.getTraffic()));
        BigDecimal communicationDate = new BigDecimal(Double.toString(salaryParam.getCommunication()));
        BigDecimal mealDate = new BigDecimal(Double.toString(salaryParam.getMeal()));
        BigDecimal housingDate = new BigDecimal(Double.toString(salaryParam.getHousing()));
        BigDecimal travelDate = new BigDecimal(Double.toString(salaryParam.getTravel()));
        BigDecimal overtimeDate = new BigDecimal(Double.toString(salaryParam.getOvertime()));
        return baseDate.add(bonusDate).add(trafficDate).add(communicationDate).add(mealDate).add(housingDate).add(travelDate).add(overtimeDate).doubleValue();
    }

    //明细
    @RequestMapping("/detail/{salaryId}")
    public String detail(Model model, @PathVariable String salaryId) {
        System.out.println("salaryId = " + salaryId);
        SalaryPO salaryPO = salaryStandardService.findOne(salaryId);
        model.addAttribute("salaryRes", salaryPO);
        return "SalaryStandardDetail";
    }

    //删除
    @RequestMapping("/delSalary")
    @ResponseBody
    public boolean delSalary(@RequestParam String salaryId) {
        boolean b = salaryStandardService.delSalary(salaryId);
        return b;
    }

    //更新跳转
    @RequestMapping("/updateDetail/{salaryId}")
    public String updateDetail(Model model, @PathVariable String salaryId) {
        System.out.println("salaryId = " + salaryId);
        SalaryPO salaryPO = salaryStandardService.findOne(salaryId);
        model.addAttribute("salaryRes", salaryPO);
        return "SalaryStandardUpdate";
    }

    /**
     *更新
     * @param salaryPO
     * @return
     */
    @RequestMapping("/updateSalary")
    @ResponseBody
    public boolean updateSalary(@ModelAttribute SalaryPO salaryPO) {
        double caculateSum = getCaculateSum(salaryPO);
        salaryPO.setGross(caculateSum);
        return salaryStandardService.updateSalary(salaryPO);
    }

    /**
     * 提交
     * @param salaryId
     * @return
     */
    @RequestMapping("/review")
    @ResponseBody
    public boolean review(@RequestParam String salaryId) {
        return salaryStandardService.review(salaryId);
    }

    /**
     *更改
     * @param model
     * @param salaryId
     * @return
     */
    @RequestMapping("/change/{salaryId}")
    public String change(Model model, @PathVariable String salaryId) {
        SalaryPO salaryPO = salaryStandardService.findOne(salaryId);
        model.addAttribute("salaryRes", salaryPO);
        return "SalaryStandardChange";
    }

    /**
     * 复核跳转
     *
     * @param model
     * @param salaryId
     * @return
     */
    @RequestMapping("/check/{salaryId}")
    public String check(Model model, @PathVariable String salaryId) {
        System.out.println("salaryId = " + salaryId);
        model.addAttribute("salaryId", salaryId);
        return "SalaryStandardCheck";
    }


    @RequestMapping("/checkDate")
    @ResponseBody
    public boolean checkDate(@ModelAttribute SalaryPO salaryPO) {
        return salaryStandardService.checkDate(salaryPO.getSalaryId(), salaryPO.getReviewComments());
    }

}
